<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>作用域链 | JavaScript高级讲义</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../Sources/闭包.html" />
    
    
    <link rel="prev" href="../Sources/变量名提升.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="1.2.3"
        data-chapter-title="作用域链"
        data-filepath="Sources/作用域链.md"
        data-basepath=".."
        data-revision="Mon Oct 24 2016 01:31:56 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Introduction
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" >
            
            <span><b>1.</b> JavaScript高级知识</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="Sources/递归.html">
            
                
                    <a href="../Sources/递归.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        递归
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
            <span><b>1.2.</b> 词法作用域</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="Sources/词法作用域.html">
            
                
                    <a href="../Sources/词法作用域.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.1.</b>
                        
                        词法作用域
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="Sources/变量名提升.html">
            
                
                    <a href="../Sources/变量名提升.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.2.</b>
                        
                        变量名提升
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="1.2.3" data-path="Sources/作用域链.html">
            
                
                    <a href="../Sources/作用域链.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.3.</b>
                        
                        作用域链
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="Sources/闭包.html">
            
                
                    <a href="../Sources/闭包.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        闭包
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="Sources/函数的四种调用模式.html">
            
                
                    <a href="../Sources/函数的四种调用模式.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        函数的四种调用模式
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" >
            
            <span><b>2.</b> 面向对象案例</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" >
            
            <span><b>2.1.</b> 歌曲列表管理</span>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" >
            
            <span><b>2.2.</b> Tab栏切换</span>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >JavaScript高级讲义</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="&#x4F5C;&#x7528;&#x57DF;&#x94FE;">&#x4F5C;&#x7528;&#x57DF;&#x94FE;</h2>
<h3 id="&#x4EC0;&#x4E48;&#x662F;&#x4F5C;&#x7528;&#x57DF;&#x94FE;">&#x4EC0;&#x4E48;&#x662F;&#x4F5C;&#x7528;&#x57DF;&#x94FE;</h3>
<p>&#x53EA;&#x6709;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5236;&#x9020;&#x4F5C;&#x7528;&#x57DF;&#x7ED3;&#x6784;&#xFF0C; &#x90A3;&#x4E48;&#x53EA;&#x8981;&#x662F;&#x4EE3;&#x7801;&#xFF0C;&#x5C31;&#x81F3;&#x5C11;&#x6709;&#x4E00;&#x4E2A;&#x4F5C;&#x7528;&#x57DF;, &#x5373;&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;&#x3002;</p>
<p>&#x51E1;&#x662F;&#x4EE3;&#x7801;&#x4E2D;&#x6709;&#x51FD;&#x6570;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x5C31;&#x6784;&#x6210;&#x53E6;&#x4E00;&#x4E2A;&#x4F5C;&#x7528;&#x57DF;&#x3002;&#x5982;&#x679C;&#x51FD;&#x6570;&#x4E2D;&#x8FD8;&#x6709;&#x51FD;&#x6570;&#xFF0C;&#x90A3;&#x4E48;&#x5728;&#x8FD9;&#x4E2A;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;&#x5C31;&#x53C8;&#x53EF;&#x4EE5;&#x8BDE;&#x751F;&#x4E00;&#x4E2A;&#x4F5C;&#x7528;&#x57DF;&#x3002;</p>
<p>&#x5C06;&#x8FD9;&#x6837;&#x7684;&#x6240;&#x6709;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x5217;&#x51FA;&#x6765;&#xFF0C;&#x53EF;&#x4EE5;&#x6709;&#x4E00;&#x4E2A;&#x7ED3;&#x6784;: &#x51FD;&#x6570;&#x5185;&#x6307;&#x5411;&#x51FD;&#x6570;&#x5916;&#x7684;&#x94FE;&#x5F0F;&#x7ED3;&#x6784;&#x3002;&#x5C31;&#x79F0;&#x4F5C;&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#x3002;</p>
<p><strong>&#x4F8B;&#x5982;&#xFF1A;</strong></p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f1</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f2</span>(<span class="hljs-params"></span>) </span>{
    }
}

<span class="hljs-keyword">var</span> num = <span class="hljs-number">456</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f3</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f4</span>(<span class="hljs-params"></span>) </span>{    
    }
}
</code></pre>
<p><img src="../Images/06-1.png" alt=""></p>
<h3 id="&#x7ED8;&#x5236;&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#x7684;&#x6B65;&#x9AA4;">&#x7ED8;&#x5236;&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#x7684;&#x6B65;&#x9AA4;:</h3>
<ol>
<li><p>&#x770B;&#x6574;&#x4E2A;&#x5168;&#x5C40;&#x662F;&#x4E00;&#x6761;&#x94FE;, &#x5373;&#x9876;&#x7EA7;&#x94FE;, &#x8BB0;&#x4E3A; 0 &#x7EA7;&#x94FE;</p>
</li>
<li><p>&#x770B;&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;, &#x6709;&#x4EC0;&#x4E48;&#x6210;&#x5458;&#x58F0;&#x660E;, &#x5C31;&#x4EE5;&#x65B9;&#x683C;&#x7684;&#x5F62;&#x5F0F;&#x7ED8;&#x5236;&#x5230; 0 &#x7EA7;&#x7EC3;&#x4E0A;</p>
</li>
<li><p>&#x518D;&#x627E;&#x51FD;&#x6570;, &#x53EA;&#x6709;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x9650;&#x5236;&#x4F5C;&#x7528;&#x57DF;, &#x56E0;&#x6B64;&#x4ECE;&#x51FD;&#x6570;&#x4E2D;&#x5F15;&#x5165;&#x65B0;&#x94FE;, &#x6807;&#x8BB0;&#x4E3A; 1 &#x7EA7;&#x94FE;</p>
</li>
<li><p>&#x7136;&#x540E;&#x5728;&#x6BCF;&#x4E00;&#x4E2A; 1 &#x7EA7;&#x94FE;&#x4E2D;&#x518D;&#x6B21;&#x5F80;&#x590D;&#x521A;&#x624D;&#x7684;&#x884C;&#x4E3A;</p>
</li>
</ol>
<h3 id="&#x53D8;&#x91CF;&#x7684;&#x8BBF;&#x95EE;&#x89C4;&#x5219;">&#x53D8;&#x91CF;&#x7684;&#x8BBF;&#x95EE;&#x89C4;&#x5219;</h3>
<ul>
<li><p>&#x9996;&#x5148;&#x770B;&#x53D8;&#x91CF;&#x5728;&#x7B2C;&#x51E0;&#x6761;&#x94FE;&#x4E0A;, &#x5728;&#x8BE5;&#x94FE;&#x4E0A;&#x770B;&#x662F;&#x5426;&#x6709;&#x53D8;&#x91CF;&#x7684;&#x5B9A;&#x4E49;&#x4E0E;&#x8D4B;&#x503C;, &#x5982;&#x679C;&#x6709;&#x76F4;&#x63A5;&#x4F7F;&#x7528;</p>
</li>
<li><p>&#x5982;&#x679C;&#x6CA1;&#x6709;&#x5230;&#x4E0A;&#x4E00;&#x7EA7;&#x94FE;&#x4E0A;&#x627E;( n - 1 &#x7EA7;&#x94FE; ), &#x5982;&#x679C;&#x6709;&#x76F4;&#x63A5;&#x7528;, &#x505C;&#x6B62;&#x7EE7;&#x7EED;&#x67E5;&#x627E;.</p>
</li>
<li><p>&#x5982;&#x679C;&#x8FD8;&#x6CA1;&#x6709;&#x518D;&#x6B21;&#x5F80;&#x4E0A;&#x521A;&#x627E;... &#x76F4;&#x5230;&#x5168;&#x5C40;&#x94FE;( 0 &#x7EA7; ), &#x8FD8;&#x6CA1;&#x6709;&#x5C31;&#x662F; is not defined</p>
</li>
<li><p>&#x6CE8;&#x610F;,&#x540C;&#x7EA7;&#x7684;&#x94FE;&#x4E0D;&#x53EF;&#x6DF7;&#x5408;&#x67E5;&#x627E;</p>
</li>
</ul>
<h4 id="&#x7EC3;&#x4E60;&#xFF1A;&#x7ED8;&#x5236;&#x4F5C;&#x7528;&#x57DF;&#x94FE;">&#x7EC3;&#x4E60;&#xFF1A;&#x7ED8;&#x5236;&#x4F5C;&#x7528;&#x57DF;&#x94FE;</h4>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f1</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> num = <span class="hljs-number">123</span>;
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f2</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log( num );
    }
    f2();
}

<span class="hljs-keyword">var</span> num = <span class="hljs-number">456</span>;
f1();
</code></pre>
<p><img src="../Images/06-2.png" alt=""></p>
<h3 id="&#x5982;&#x4F55;&#x5206;&#x6790;&#x4EE3;&#x7801;">&#x5982;&#x4F55;&#x5206;&#x6790;&#x4EE3;&#x7801;</h3>
<ol>
<li>&#x5728;&#x5206;&#x6790;&#x4EE3;&#x7801;&#x7684;&#x65F6;&#x5019;&#x5207;&#x8BB0;&#x4ECE;&#x4EE3;&#x7801;&#x7684;&#x8FD0;&#x884C;&#x8FDB;&#x5EA6;&#x4E0A;&#x6765;&#x5206;&#x6790;, &#x5982;&#x679C;&#x4EE3;&#x7801;&#x7ED9;&#x53D8;&#x91CF;&#x8D4B;&#x503C;&#x4E86;, &#x4E00;&#x5B9A;&#x8981;&#x6807;&#x8BB0;&#x5230;&#x56FE;&#x4E2D;</li>
<li>&#x5982;&#x679C;&#x4EE3;&#x7801;&#x6BD4;&#x8F83;&#x590D;&#x6742;, &#x53EF;&#x4EE5;&#x5728;&#x56FE;&#x4E2D;&#x63CF;&#x8FF0;&#x4EE3;&#x7801;&#x7684;&#x5185;&#x5BB9;, &#x6709;&#x4E8B;&#x751A;&#x81F3;&#x9700;&#x8981;&#x5C06;&#x539F;&#x578B;&#x56FE;&#x4E0E;&#x4F5C;&#x7528;&#x57DF;&#x56FE;&#x5408;&#x5E76;&#x5206;&#x6790;</li>
</ol>
<h4 id="&#x7EC3;&#x4E60;">&#x7EC3;&#x4E60;</h4>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> num = <span class="hljs-number">123</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f1</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log( num );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f2</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> num = <span class="hljs-number">456</span>;
    f1();
}
f2();
</code></pre>
<p><img src="../Images/06-3.png" alt=""></p>
<h4 id="&#x7EC3;&#x4E60;">&#x7EC3;&#x4E60;</h4>
<pre><code class="lang-js">
<span class="hljs-keyword">var</span> num = <span class="hljs-number">123</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f1</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log( num );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f2</span>(<span class="hljs-params"></span>) </span>{
    num = <span class="hljs-number">456</span>;
    f1();
}

f2();
</code></pre>
<h3 id="&#x8865;&#x5145;">&#x8865;&#x5145;</h3>
<font color="red"> &#x58F0;&#x660E;&#x53D8;&#x91CF;&#x4F7F;&#x7528;`var`, &#x5982;&#x679C;&#x4E0D;&#x4F7F;&#x7528;`var`&#x58F0;&#x660E;&#x7684;&#x53D8;&#x91CF;&#x5C31;&#x662F;&#x5168;&#x5C40;&#x53D8;&#x91CF;( &#x7981;&#x7528; )</font>

<p>&#x56E0;&#x4E3A;&#x5728;&#x4EFB;&#x4F55;&#x4EE3;&#x7801;&#x7ED3;&#x6784;&#x4E2D;&#x90FD;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x8BE5;&#x8BED;&#x6CD5;. &#x90A3;&#x4E48;&#x518D;&#x4EE3;&#x7801;&#x7EF4;&#x62A4;&#x7684;&#x65F6;&#x5019;&#x4F1A;&#x6709;&#x95EE;&#x9898;. &#x6240;&#x4EE5;&#x9664;&#x975E;&#x7279;&#x6B8A;&#x539F;&#x56E0;&#x4E0D;&#x8981;&#x8FD9;&#x4E48;&#x7528;.</p>
<p>&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x7684;&#x9519;&#x8BEF;</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> i1 = <span class="hljs-number">1</span> <span class="hljs-comment">// &#x5C40;&#x90E8;</span>
    i2 = <span class="hljs-number">2</span>, <span class="hljs-comment">// &#x5168;&#x5C40;</span>
    i3 = <span class="hljs-number">3</span>; <span class="hljs-comment">// &#x5168;&#x5C40;</span>
}
</code></pre>
<p>&#x6B64;&#x65F6;&#x6CE8;&#x610F;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> arr = [];
<span class="hljs-keyword">for</span> ( <span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++ ) {
    arr.push( i );
}

<span class="hljs-keyword">for</span> ( <span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++ ) {
    <span class="hljs-built_in">console</span>.log( arr[ i ] );
}

<span class="hljs-comment">// &#x4E00;&#x822C;&#x90FD;&#x662F;&#x5C06;&#x53D8;&#x91CF;&#x7684;&#x58F0;&#x660E;&#x5168;&#x90E8;&#x653E;&#x5230;&#x5F00;&#x59CB;&#x7684;&#x4F4D;&#x7F6E;, &#x907F;&#x514D;&#x51FA;&#x73B0;&#x56E0;&#x4E3A;&#x63D0;&#x5347;&#x800C;&#x9020;&#x6210;&#x7684;&#x9519;&#x8BEF;</span>
<span class="hljs-keyword">var</span> arr = [],
i = <span class="hljs-number">0</span>;

<span class="hljs-keyword">for</span> ( ; i &lt; <span class="hljs-number">10</span>; i++ ) {
    arr.push( i );
}

<span class="hljs-keyword">for</span> ( i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++ ) {
    <span class="hljs-built_in">console</span>.log( arr[ i ] );
}
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../Sources/变量名提升.html" class="navigation navigation-prev " aria-label="Previous page: 变量名提升"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../Sources/闭包.html" class="navigation navigation-next " aria-label="Next page: 闭包"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
